<template>
  <div >
    <el-row>
      <el-col :span="18">
        <!-- 工具栏 -->
        <el-row>
          <p>说明: 此工具用于开发给运营导数据用, 导出大量后台数据, 所有表名需要带上 schema 信息, 这里可以导出百万级的数据量</p>
          <el-button-group class="">
            <el-button :disabled="!connName || !sql" size="small" @click="preview">预览</el-button>
            <el-button :disabled="!connName || !sql" type="primary" size="small" @click="exportData">数据导出(Excel)</el-button>
          </el-button-group>
        </el-row>

        <!-- sql 书写区 -->
        <el-row class="margin-top">
          <el-col :span="24">
            <el-input
              v-model="sql"
              type="textarea"
              :autosize="{ minRows: 5, maxRows: 15}"
              placeholder="写入你的 sql 语句"
            />
          </el-col>
        </el-row>

        <!-- 数据展示区 -->
        <el-row class="margin-top ">
          <span v-if="total">数据量: {{total}} </span>
          <span>预览只会展示前 15 行数据, 使用导出来导出所有数据</span>
          <el-table
            :data="dynamicTableData.rows"
            border
            stripe
            style="width: 100%"
            highlight-current-row
            fit
            height="500"
            size="mini"
            class="nowrap-tab"
          >
            <el-table-column
              type="selection"
              width="55" />
            <el-table-column
              v-for="header in dynamicTableData.headers"
              :key="header.columnName"
              :prop="header.columnName"
              :label="header.columnName"
              :width="header.columnName.length * 20 < 100 ? 100 : header.columnName.length * 20 "
              :show-overflow-tooltip="true"
              sortable
              resizable
            >
              <template slot="header" slot-scope="{row}">
                <div class="column-header">
                  <span ><b >{{header.dataType}}</b> {{header.columnName}}</span>
                </div>
              </template>
              <template slot-scope="{row}">
                <div contenteditable="true">{{row[header.columnName]}}</div>
              </template>
            </el-table-column>

          </el-table>
        </el-row>
      </el-col>
      <el-col :span="6">
        <div class="padding">
          <TableSearch ref="tableSearch" :conn-name="connName" :namespace="namespace" />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import core from '../../../api/core'
import database from '../../../api/database'

import TableSearch from '@/views/database/TableSearch'

export default {
  name: 'DataExport',
  components: { TableSearch },
  props: {
    connName: String,
    namespace: Object
  },
  data() {
    return {
      sql: null,
      dynamicTableData: {},
      total: 0
    }
  },
  methods: {
    preview() {
      database.exportPreview(this.connName,this.namespace, this.sql).then(res => {
        this.dynamicTableData = res.data.body
        this.total = res.data.total;
      })
    },
    exportData() {
      database.exportData(this.connName, this.namespace,this.sql).then(res => {
        core.fileDownload(res.data.relativePath)
      })
    }
  },
  watch: {
    connName(){
    },
    namespace(){
      this.$refs.tableSearch.searchTables();
    }
  }
}
</script>

<style scoped>
.nowrap-tab .el-table__header .cell{
  padding: 0!important;
  text-overflow: unset!important;
  white-space:nowrap!important
}

.column-header{
  display: inline-block;
}

.column-header>span{
  font-size: 12px;

  overflow-x: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.column-header>span>b{
  color: #1d8ce0;
  transform: scale(0.5);
}

</style>
